'头结点及结点个数

Public HeadNode As BinaryTreeItem

Public iLength As Integer

'初始化

Private Sub Class_Initialize()

    initBinaryTree

End Sub

'初始化树

Sub initBinaryTree()

    Set HeadNode = Nothing

    iLength = 0

End Sub

'使用前序遍历生成二叉树

Function CreateBinaryTree(PreOrder1() As String * 1, i As Integer, length As Integer)

    Dim newNode As BinaryTreeItem

    Dim ch As String * 1

    Set newNode = Nothing

    If i <= length Then

        ch = PreOrder1(i)

        i = i + 1

        If (ch <> "#") Then

            Set newNode = New BinaryTreeItem

            newNode.Value = ch

            Set newNode.LeftChild = CreateBinaryTree(PreOrder1, i, length)

            Set newNode.RightChild = CreateBinaryTree(PreOrder1, i, length)

        End If

    End If

    Set CreateBinaryTree = newNode

End Function

'前序遍历

Sub PreOrder(Node As BinaryTreeItem)

    If (Not Node Is Nothing) Then

        Debug.Print Node.Value

        Call PreOrder(Node.LeftChild)

        Call PreOrder(Node.RightChild)

    End If

End Sub
